// 基础api

export function createStore(reducer) {
  let currentState = {}
  let currentListeners = []

  function getState() {
    return currentState
  }

  function subscribe(listener) {
    return currentListeners.push(listener)
  }

  function dispatch(action) {
    currentState = reducer(currentState, action)
    currentListeners.forEach(v => v())
    return action
  }

  dispatch({type: "@132yse/SMox"})
  return {getState, subscribe, dispatch}
}


export function bindActionCreators(creators, dispatch) {
  return Object.keys(creators).reduce((ret,item)=>{
    ret[item] = bindActionCreator(creators[item],dispatch)
    return ret
  },{})
}

function bindActionCreator(creator, dispatch){
  return (...args) => dispatch(creator(...args))
}